//
//  EUExDemo.m
//  EUExDemo
//
//  Created by hui.li on 13-4-11.
//  Copyright (c) 2013年 hui.li. All rights reserved.
//

#import "EUExXG.h"
#import "EUtility.h"
#import "XGPush.h"
#import "XGSetting.h"
#define _IPHONE80_ 80000

@implementation EUExXG


-(id)initWithBrwView:(EBrowserView *)eInBrwView{
    self = [super initWithBrwView:eInBrwView];
    if (self) {
    }
    return self;
}

//初始化信鸽推送
-(void)InitXgPush:(NSMutableArray *)array{
    NSLog(@"in InitXgPush");
    
    //NSLog(@"%d",[[array objectAtIndex:0] intValue]);
    //     NSLog([array objectAtIndex:1]);
    double appid =[[array objectAtIndex:0] doubleValue];
    NSString *appkey =[array objectAtIndex:1];
    [XGPush startApp:appid appKey:appkey];
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
    [self InitXGPUSH];//调用通用的注册方法。
    
    [self.meBrwView stringByEvaluatingJavaScriptFromString:@"uexXG.cbXgPush(0,0,\"InitXgPush\");"];
}


//初始化推送为用户绑定方式。
-(void)InitXgUserPush:(NSMutableArray *)array{
    NSLog(@"in InitXgUserPush");
    double appid =[[array objectAtIndex:0] doubleValue];
    NSString *appkey =[array objectAtIndex:1];
    [XGPush startApp:appid appKey:appkey];
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
    [self InitXGPUSH];//调用通用的注册方法。
    [XGPush setAccount:[array objectAtIndex:2]];//绑定注册者的账号。
    
    [self.meBrwView stringByEvaluatingJavaScriptFromString:@"uexXG.cbXgPush(0,0,'InitXgUserPush');"];
}

//绑定信鸽的标签。
-(void)SetXgTag:(NSMutableArray *)array{
    if ([array isKindOfClass:[NSMutableArray class]] && [array count]>0) {
    }
    NSLog(@"in SetXgTag");
    
    NSString *tag = [array objectAtIndex:0];
    
    
    [XGPush setTag:tag];
    //为不同的"用户"设置标签
    
    //
    //    void (^successBlock)(void) = ^(void){
    //        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"信鸽推送"
    //                                                        message:@"设置标签成功"
    //                                                       delegate:nil
    //                                              cancelButtonTitle:@"OK"
    //                                              otherButtonTitles:nil];
    //        [alert show];
    //    };
    //
    //    void (^errorBlock)(void) = ^(void){
    //        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"信鸽推送"
    //                                                        message:@"设置标签失败"
    //                                                       delegate:nil
    //                                              cancelButtonTitle:@"OK"
    //                                              otherButtonTitles:nil];
    //        [alert show];
    //    };
    //
    //    [XGPush setTag:tag successCallback:successBlock errorCallback:errorBlock];
    
    //[self.meBrwView stringByEvaluatingJavaScriptFromString:@"uexXG.cbXgPush(0,0,'InitXgPush');"];
}

-(void)SetInstallChannel:(NSMutableArray *)array{
    NSLog(@"in SetInstallChannel");
    //[XGPush setTag:[array objectAtIndex:0] successCallback:nil errorCallback:nil];
    
    
    
    //[self.meBrwView stringByEvaluatingJavaScriptFromString:@"uexXG.cbXgPush(0,0,'SetInstallChannel');"];
}


-(void)DelXgTag:(NSMutableArray *)array{
    NSLog(@"in DelXgTag");
    
    [XGPush delTag:[array objectAtIndex:0] successCallback:nil errorCallback:nil];
    //[self.meBrwView stringByEvaluatingJavaScriptFromString:@"uexXG.cbXgPush(0,0,'DelXgTag');"];
}

//当前窗口调用uexWindow.close()接口的时候 插件的clean方法会被调用
-(void)clean{
    
}



- (void)registerPushForIOS8{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_
    
    //Types
    UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    
    //Actions
    UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];
    
    acceptAction.identifier = @"ACCEPT_IDENTIFIER";
    acceptAction.title = @"Accept";
    
    acceptAction.activationMode = UIUserNotificationActivationModeForeground;
    acceptAction.destructive = NO;
    acceptAction.authenticationRequired = NO;
    
    //Categories
    UIMutableUserNotificationCategory *inviteCategory = [[UIMutableUserNotificationCategory alloc] init];
    
    inviteCategory.identifier = @"INVITE_CATEGORY";
    
    [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextDefault];
    
    [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextMinimal];
    
    NSSet *categories = [NSSet setWithObjects:inviteCategory, nil];
    
    
    UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:categories];
    
    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
    
    
    [[UIApplication sharedApplication] registerForRemoteNotifications];
#endif
}

- (void)registerPush{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}


- (void)application:(UIApplication *)application RegistrDeviceToken:(NSData *)deviceToken {
    NSString * deviceTokenStr = [XGPush registerDevice:deviceToken];
    
    void (^successBlock)(void) = ^(void){
        //成功之后的处理
        NSLog(@"[XGPush]register successBlock ,deviceToken: %@",deviceTokenStr);
    };
    
    void (^errorBlock)(void) = ^(void){
        //失败之后的处理
        NSLog(@"[XGPush]register errorBlock");
    };
    
    //注册设备
    [[XGSetting getInstance] setChannel:@"appstore"];
    [XGPush registerDevice:deviceToken successCallback:successBlock errorCallback:errorBlock];
    
    //如果不需要回调
    //[XGPush registerDevice:deviceToken];
    
    //打印获取的deviceToken的字符串
    NSLog(@"deviceTokenStr is %@",deviceTokenStr);
}

//如果deviceToken获取不到会进入此事件
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
    
    NSString *str = [NSString stringWithFormat: @"Error: %@",err];
    
    NSLog(@"%@",str);
    
}



@end
